import { createRouter, createMemoryHistory, createWebHistory, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'
import GoodsView from '../views/GoodsView.vue'
// 模拟权限：假设 admin 才能访问商品管理
function checkAuth(to, from, next) {
  const userRole = localStorage.getItem('userRole')
  if (to.meta.requiresAuth) {
    if (userRole) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView
    },
    {
      path: '/goods',
      name: 'goods',
      component: GoodsView,
      meta: { requiresAuth: true } // 需要登录
    }
  ]
}

router.beforeEach(checkAuth)

export default router